import { DrawType } from '@/utils/json2image.js';

export const getAgentJson = (dataInfo, json2image) => {
  let jsonData = [];
  if (!dataInfo) {
    return jsonData;
  }
  console.log('--getAgentJson--', dataInfo);

  const width = json2image.width;
  const height = json2image.height;
  // 背景图
  let bgItem = {
    type: DrawType.IMAGE,
    url: dataInfo.image,
    left: 0,
    top: 0,
    width: width,
    height: height
  };
  jsonData.push(bgItem);

  // 公司Logo和公司名称
  if (dataInfo.showCompanyLogo == 1) {
    const companyNameTextColorMap = {
      '1': '#fff',
      '2': '#fff',
      '3': '#333',
      '4': '#fff',
      '5': '#fff',
      '6': '#fff',
      '7': '#333',
      '8': '#333',
      '9': '#fff',
      '10': '#333',
      '11': '#fff',
    };
    let logoImgItem = {
      type: DrawType.IMAGE,
      url: dataInfo.companyLogo,
      left: dataInfo.logoPosX || 0,
      top: dataInfo.logoPosY || 0,
      width: 72,
      height: 72,
      radius: '50%'
    };
    jsonData.push(logoImgItem);
    let companyNameTextItem = {
      type: DrawType.TEXT,
      content: dataInfo.companyAuth.companyName,
      fontSize: 30,
      textAlign: 'left',
      textBaseline: 'middle',
      color: companyNameTextColorMap[dataInfo.initSort] || '#fff',
      left: dataInfo.logoPosX + 72 + 6,
      top: dataInfo.logoPosY + 72 / 2,
      maxWidth: 600,
      maxLine: 1
    };
    jsonData.push(companyNameTextItem);
  }
  // 头像昵称 一排显示的
  if (dataInfo.isShowUserInfo == 1) {
    const nicknameColorMap = {
      '1': '#333',
      '2': '#fff',
      '3': '#333',
      '4': '#fff',
      '5': '#fff',
      '6': '#fff',
      '7': '#333',
      '8': '#333',
      '9': '#fff',
      '10': '#fff',
      '11': '#333',
    };
    const avatarBoxBorderMap = {
      4: 72,
      5: 60,
      7: 72,
      8: 60
    };
    if (avatarBoxBorderMap[dataInfo.initSort]) {
      let avatarBorderItem = {
        type: DrawType.RECT,
        color: '#fff',
        left: dataInfo.userAvatarPosX,
        top: dataInfo.userAvatarPosY,
        width: avatarBoxBorderMap[dataInfo.initSort],
        height: avatarBoxBorderMap[dataInfo.initSort],
        radius: '50%'
      };
      jsonData.push(avatarBorderItem);
    }
    const avatarSizeMap = {
      1: 98,
      2: 98,
      3: 60,
      4: 68,
      5: 56,
      6: 60,
      7: 68,
      8: 56,
      9: 98,
      10: 98,
      11: 98,
    };
    const avatarLeftMap = {
      1: dataInfo.userAvatarPosX,
      2: dataInfo.userAvatarPosX,
      3: dataInfo.userAvatarPosX,
      4: dataInfo.userAvatarPosX + 2,
      5: dataInfo.userAvatarPosX + 2,
      6: dataInfo.userAvatarPosX,
      7: dataInfo.userAvatarPosX + 2,
      8: dataInfo.userAvatarPosX + 2,
      9: dataInfo.userAvatarPosX,
      10: dataInfo.userAvatarPosX,
      11: dataInfo.userAvatarPosX,
    };
    const avatarTopMap = {
      1: dataInfo.userAvatarPosY,
      2: dataInfo.userAvatarPosY,
      3: dataInfo.userAvatarPosY,
      4: dataInfo.userAvatarPosY + 2,
      5: dataInfo.userAvatarPosY + 2,
      6: dataInfo.userAvatarPosY,
      7: dataInfo.userAvatarPosY + 2,
      8: dataInfo.userAvatarPosY + 2,
      9: dataInfo.userAvatarPosY,
      10: dataInfo.userAvatarPosY,
      11: dataInfo.userAvatarPosY,
    };
    let avatarItem = {
      type: DrawType.IMAGE,
      url: dataInfo.avatar,
      left: avatarLeftMap[dataInfo.initSort] || dataInfo.userAvatarPosX,
      top: avatarTopMap[dataInfo.initSort] || dataInfo.userAvatarPosY,
      width: avatarSizeMap[dataInfo.initSort] || 98,
      height: avatarSizeMap[dataInfo.initSort] || 98,
      radius: '50%'
    };
    jsonData.push(avatarItem);
    const nicknameTextAlignMap = {
      1: 'center',
      2: 'left',
      3: 'center',
      4: 'left',
      5: 'center',
      6: 'center',
      7: 'left',
      8: 'center',
      9: 'center',
      10: 'center',
      11: 'center',
    };
    const nicknameLeftMap = {
      1: dataInfo.userAvatarPosX + 98 / 2,
      2: dataInfo.userAvatarPosX,
      3: dataInfo.userAvatarPosX + 60 / 2,
      4: dataInfo.userAvatarPosX + 72 + 20, //在头像后面20px
      5: dataInfo.userAvatarPosX + 60 / 2,
      6: dataInfo.userAvatarPosX + 60 / 2,
      7: dataInfo.userAvatarPosX + 72 + 20, //在头像后面20px
      8: dataInfo.userAvatarPosX + 60 / 2,
      9: dataInfo.userAvatarPosX + 98 / 2,
      10: dataInfo.userAvatarPosX + 98 / 2,
      11: dataInfo.userAvatarPosX + 98 / 2,
    };
    const nicknameTopMap = {
      1: dataInfo.userAvatarPosY + 98 + 10,
      2: dataInfo.userAvatarPosY + 98 + 10,
      3: dataInfo.userAvatarPosY + 60 + 10,
      4: dataInfo.userAvatarPosY + 72 / 2 - 28 / 2, //和头像居中对齐
      5: dataInfo.userAvatarPosY + 60 + 20,
      6: dataInfo.userAvatarPosY + 60 + 20,
      7: dataInfo.userAvatarPosY + 72 / 2 - 28 / 2, //和头像居中对齐
      8: dataInfo.userAvatarPosY + 60 + 20,
      9: dataInfo.userAvatarPosY + 98 + 10,
      10: dataInfo.userAvatarPosY + 98 + 10,
      11: dataInfo.userAvatarPosY + 98 + 10,
    };
    let nicknameTextItem = {
      type: DrawType.TEXT,
      content: dataInfo.nickname,
      left: nicknameLeftMap[dataInfo.initSort],
      top: nicknameTopMap[dataInfo.initSort],
      fontSize: 28,
      textAlign: nicknameTextAlignMap[dataInfo.initSort] || 'center',
      color: nicknameColorMap[dataInfo.initSort] || '#fff'
    };
    jsonData.push(nicknameTextItem);
  }
  //第7张图文案需要旋转-2deg
  if (dataInfo.initSort == 7) {
    let rotateItem = {
      type: DrawType.ROTATE,
      deg:-2
    }
    jsonData.push(rotateItem);
  }
  if (dataInfo.textType == 1) {
    //默认文案
    let nicknameTextItem = {
      type: DrawType.TEXT,
      content: dataInfo.wordText,
      left: dataInfo.text0PosX,
      top: dataInfo.text0PosY,
      fontSize: (dataInfo.sizeNum / 0.34) * 0.8,
      textAlign: 'left',
      weight: dataInfo.fontWeight == 1 ? 600 : 400,
      color: dataInfo.textColor,
      maxLine: 1
    };
    jsonData.push(nicknameTextItem);
  } else {
    //自定义文案
    if (dataInfo.text1) {
      let word1TextItem = {
        type: DrawType.TEXT,
        content: dataInfo.text1,
        left: dataInfo.text1PosX,
        top: dataInfo.text1PosY,
        fontSize: (dataInfo.sizeNum / 0.34) * 0.8,
        textAlign: 'left',
        weight: dataInfo.fontWeight == 1 ? 600 : 400,
        color: dataInfo.textColor,
        maxLine: 1,
        maxWidth: width
      };
      jsonData.push(word1TextItem);
    }
    if (dataInfo.text2) {
      let word2TextItem = {
        type: DrawType.TEXT,
        content: dataInfo.text2,
        left: dataInfo.text2PosX,
        top: dataInfo.text2PosY,
        fontSize: (dataInfo.sizeNum / 0.34) * 0.8,
        textAlign: 'left',
        weight: dataInfo.fontWeight == 1 ? 600 : 400,
        color: dataInfo.textColor,
        maxLine: 1,
        maxWidth: width
      };
      jsonData.push(word2TextItem);
    }
    if (dataInfo.text3) {
      let word3TextItem = {
        type: DrawType.TEXT,
        content: dataInfo.text3,
        left: dataInfo.text3PosX,
        top: dataInfo.text3PosY,
        fontSize: (dataInfo.sizeNum / 0.34) * 0.8,
        textAlign: 'left',
        weight: dataInfo.fontWeight == 1 ? 600 : 400,
        color: dataInfo.textColor,
        maxLine: 1,
        maxWidth: width
      };
      jsonData.push(word3TextItem);
    }
  }
  //第7张图文案旋转完需要恢复
  if (dataInfo.initSort == 7) {
    let rotateItem = {
      type: DrawType.ROTATE,
      deg:2
    }
    jsonData.push(rotateItem);
  }
  const QR_SIZE_MAP = {
    1: 280,
    2: 240,
    3: 200
  };
  // 二维码
  let qrCodeItem = {
    type: DrawType.IMAGE,
    url: dataInfo.qrcodeImg,
    left: dataInfo.qrcodePosX,
    top: dataInfo.qrcodePosY,
    width: QR_SIZE_MAP[dataInfo.qrcodeSize] || 200,
    height: QR_SIZE_MAP[dataInfo.qrcodeSize] || 200
  };
  jsonData.push(qrCodeItem);

  //底部商家信息
  if (dataInfo.companyAuth.enableShowInfo == 1) {
    let infoBg = {
      type: DrawType.RECT,
      height: 40,
      left: 0,
      linearGradient: {
        startX: 0,
        startY: 1294,
        endX: 0,
        endY: 1334,
        colors: ['rgba(0,0,0,0.3)', 'rgba(0,0,0,0.1)']
      },
      top: 1294,
      width: 750
    };
    const instruTextColorMap = {
      1: '#fff',
      2: '#fff',
      3: '#ccc',
      4: '#ccc',
      5: '#ccc',
      6: '#ccc',
      7: '#EFEFEF',
      8: '#ccc',
      9: '#fff',
      10: '#fff',
      11: '#fff'
    }
    let showItem = {
      type: DrawType.TEXT,
      content: dataInfo.companyAuth.instruction,
      color: instruTextColorMap[dataInfo.initSort] || '#fff',
      fontSize: 22,
      left: 375,
      maxLine: 1,
      textAlign: 'center',
      textBaseline: 'middle',
      top: 1314
    };
    const bgArr = [1, 2, 9, 10, 11];
    const isOldPoster = bgArr.includes(dataInfo.initSort);
    //旧的海报显示样式
    if (isOldPoster) {
      jsonData.push(infoBg);
    }
    //新海报底部字体大小和位置不一样
    if (!isOldPoster) {
      showItem.fontSize = 26;
      showItem.top = height - 20 - 26 / 2;
    }
    jsonData.push(showItem);
  }
  return jsonData;
};
